﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KZ.Express.H.ENT;
using KZ.Utilities;
using KZ.Express.H.Presenter;
using KZ.Express.H.BLL;

namespace KZ.Express.H.Orders
{
    public partial class UpdateStandardPriceView : BaseView
    {
        private void UpdateStandardPriceView_Load(object sender, EventArgs e)
        {
            dtpFrom.Value = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);
            dtpTo.Value = new DateTime(DateTime.Today.Year, DateTime.Today.AddMonths(1).Month, 1).AddDays(-1);
            LoadCarrier();
        }

        private void cmbCarrierID_SelectedValueChanged(object sender, EventArgs e)
        {
            LoadSupplier();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                string msg = String.Empty;
                int carrierID = Utility.IntParse(cmbCarrierID.SelectedValue);
                int supplierID = Utility.IntParse(cmbSupplierID.SelectedValue);

                if (carrierID == 0)
                    msg = "Bạn phải chọn dịch vụ";
                if (supplierID == 0)
                    msg += "\nBạn phải chọn nhà cung cấp";

                if (!String.IsNullOrEmpty(msg))
                    this.ShowMessageBoxError(msg);

                OrderBLL orderBLL = new OrderBLL();
                orderBLL.UpdateStandardPrice(dtpFrom.Value, dtpTo.Value, carrierID, supplierID);

                DialogResult = DialogResult.OK;
            }
            catch (BusinessLogicException ex)
            {
                ShowMessageBoxError(ex.Message);
            }
        }

        private void LoadCarrier()
        {
            try
            {
                cmbCarrierID.SelectedValueChanged -= cmbCarrierID_SelectedValueChanged;

                cmbCarrierID.ValueMember = "CarrierID";
                cmbCarrierID.DisplayMember = "CarrierName";
                DataTable dt = _presenter.LoadDataTable<CarrierENT>(null);
                DataRow row = dt.NewRow();
                dt.Rows.InsertAt(row, 0);
                cmbCarrierID.DataSource = dt;
            }
            catch (BusinessLogicException ex)
            {
                ShowMessageBoxError(ex.Message);
            }
            finally
            {
                cmbCarrierID.SelectedValueChanged += cmbCarrierID_SelectedValueChanged;
            }
        }

        private void LoadSupplier()
        {
            try
            {
                SearchCriteria search = new SearchCriteria();
                search.Add("CarrierID", Utility.IntParse(cmbCarrierID.SelectedValue));
                cmbSupplierID.ValueMember = "SupplierID";
                cmbSupplierID.DisplayMember = "SupplierName";
                DataTable dt = _presenter.LoadDataTable<SupplierENT>(search);
                DataRow row = dt.NewRow();
                dt.Rows.InsertAt(row, 0);
                cmbSupplierID.DataSource = dt;
            }
            catch (BusinessLogicException ex)
            {
                ShowMessageBoxError(ex.Message);
            }
        }

        public UpdateStandardPriceView()
        {
            InitializeComponent();
            _presenter = new BasePresenter(this);
        }
    }
}
